#include "stdafx.h"
#include "functions.h"

#define HM_KEY WM_USER + 101
#pragma data_seg("SharedValue")
HWND    g_hWndCaller = NULL;
HHOOK   g_hHook = NULL;
#pragma data_seg()

HMODULE
WINAPI
ModuleFromAddress(
    PVOID  pv
    )
{
    MEMORY_BASIC_INFORMATION    mbi;
    HMODULE     hResultModule = NULL;
    do 
    {
         if( ::VirtualQuery( pv, &mbi, sizeof(mbi) ) )
         {
             hResultModule = ( HMODULE )mbi.AllocationBase;
             break;
         }
    } while ( FALSE );
   
        return hResultModule;
}

LRESULT
CALLBACK
KeyHookProc(
    int ncode,
    WPARAM wParam,
    LPARAM   lParam
    )
{
    do 
    {
        if( ncode < 0 || HC_NOREMOVE == ncode )
            break;

        if( lParam & 0X40000000 )
        {
            break;
        }

        ::PostMessage( g_hWndCaller, HM_KEY, wParam, lParam );
    } while ( FALSE );
    
    return ::CallNextHookEx( g_hHook, ncode, wParam, lParam );

}